﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
namespace RpgEditor
{
    public class bundledata
    {
        public String key;
        public String[] data;
    }

    public class bundle
    {
        public ArrayList data = new ArrayList();
        public void load(String fn)
        {
            int num = 0;
            if (!File.Exists(fn))
            {
                num = 0;
                return;
            }
            else
            {
                FileStream fs1 = new FileStream(fn, FileMode.Open, FileAccess.Read);
                BinaryReader dis = new BinaryReader(fs1, System.Text.Encoding.Default);

                num = MyIO.readByte(dis);

                data = new ArrayList();
                for (int i = 0; i < num; i++)
                {
                    bundledata t = new bundledata();
                    t.key = MyIO.readString(dis);
                    int dataNum = MyIO.readByte(dis);
                    t.data = new String[dataNum];
                    for (int j = 0; j < dataNum; j++)
                    {
                        t.data[j] = MyIO.readString(dis);
                    }
                    data.Add(t);
                }
                dis.Close();
            }
        }

        public void save(String fn)
        {

            if (data != null)
            {
                FileStream fs1 = new FileStream(fn, FileMode.Create, FileAccess.Write);
                BinaryWriter dis = new BinaryWriter(fs1, System.Text.Encoding.Default);
                MyIO.writeByte(dis, (SByte)data.Count);

                for (int i = 0; i < data.Count; i++)
                {
                    bundledata t = (bundledata)(data[i]);
                    MyIO.writeString(dis, t.key);
                    MyIO.writeByte(dis, (SByte)t.data.Length);

                    for (int j = 0; j < t.data.Length; j++)
                    {
                        MyIO.writeString(dis, t.data[j]);
                    }
                }
                dis.Close();
            }
        }
    }
    public class monster
    {
    public     sbyte id;
/** 显示名字 */
        public string name;
        sbyte icon;
	/** 职业 */
        public sbyte job;
	

	
	/** 生命值 */
        public short _hp;
	
	/** 魔法值 */
        public short _mp;
	
	/** 物理攻击力 */
        public short _pact;
	
	/** 物理防御力 */
        public short _pdef;
	
	/** 魔法攻击力 */
        public short _mact;
	
	/** 魔法防御力 */
        public short _mdef;
	

	
	/** 敏捷（移动力有关） */
        public short _mov;

        public sbyte it1;
        public sbyte it2;
        public sbyte it3;
        public sbyte sk1;
        public sbyte sk2;
        public sbyte sk3;

	/** 当前经验(当该角色为敌人时,该变量表示杀死该角色后获得的经验) */
        public short _exp;

        public bundle data = new bundle();
        mainForm form = null;
        private String fn;
        public monster(mainForm f)
        {
            form = f;
        }
        public monster()
        {
        }

        public monster(bundledata d)
        {
            this.id = sbyte.Parse(d.key);
            name = d.data[0];
            icon = SByte.Parse(d.data[1]);
            job = SByte.Parse(d.data[2]);
            _hp = short.Parse(d.data[3]);
            _mp = short.Parse(d.data[4]);
            _pact = short.Parse(d.data[5]);
            _mact = short.Parse(d.data[6]);
            _pdef = short.Parse(d.data[7]);
            _mdef = short.Parse(d.data[8]);
            _mov = short.Parse(d.data[9]);

            _exp = short.Parse(d.data[10]);
            it1 = SByte.Parse(d.data[11]);
            it2 = SByte.Parse(d.data[12]);
            it3 = SByte.Parse(d.data[13]);

            sk1 = SByte.Parse(d.data[14]);
            sk2 = SByte.Parse(d.data[15]);
            sk3 = SByte.Parse(d.data[16]);
        }

        public bundledata tobundledata()
        {
            bundledata d = new bundledata();
            d.data = new string[17];
            d.key = "" + id;

            d.data[0] = name;
            d.data[1] = ""+icon;
            d.data[2] = "" + job;
            d.data[3] = "" + _hp;
            d.data[4] = "" + _mp;
            d.data[5] = "" + _pact;
            d.data[6] = "" + _mact;
            d.data[7] = "" + _pdef;
            d.data[8] = "" + _mdef;
            d.data[9] = "" + _mov;

            d.data[10] = "" + _exp;
            d.data[11] = "" + it1;
            d.data[12] = "" + it2;
            d.data[13] = "" + it3;

            d.data[14] = "" + sk1;
            d.data[15] = "" + sk2;
            d.data[16] = "" + sk3;
            return d;
        }

        public void setFn(String fn)
        {
            this.fn = fn;
            form.textBox63.Text = fn;
        }

        public String getFn()
        {
            return fn;
        }

        public void save()
        {
            data.save(fn);
        }

        public void getInfo(String fn)
        {

            data.load(fn);

        }
        public void init()
        {

        }
        public void display()
        {

            form.listBox13.Items.Clear();
            ArrayList list = data.data;
            for (int i = 0; i < list.Count; i++)
            {
                bundledata t = (bundledata)(list[i]);
                monster s = new monster(t);
                form.listBox13.Items.Add(s.id + "," + s.name);
            }
            if (list.Count > 0)
            {
                form.listBox13.SelectedIndex = 0;

                displayatt(0);
            }
        }

        public void displayatt(int i)
        {
            if (i >= data.data.Count)
            {
                return;
            }
            ArrayList list = data.data;
            bundledata t = (bundledata)list[i];
            monster s = new monster(t);
            form.textBox72.Text = "" + s.id;
            form.textBox71.Text = "" + s.name;
            form.textBox80.Text = "" + s.icon;
            form.comboBox15.SelectedIndex = s.job;
            form.textBox70.Text = "" + s._hp;
            form.textBox69.Text = "" + s._mp;

            form.textBox68.Text = "" + s._pact;
            form.textBox67.Text = "" + s._mact;
            form.textBox66.Text = "" + s._pdef;
            form.textBox65.Text = "" + s._mdef;
            form.textBox64.Text = "" + s._mov;

            form.textBox73.Text = "" + s._exp;

            form.textBox74.Text = "" + s.it1;
            form.textBox75.Text = "" + s.it2;
            form.textBox76.Text = "" + s.it3;
            form.textBox77.Text = "" + s.sk1;
            form.textBox78.Text = "" + s.sk2;
            form.textBox79.Text = "" + s.sk3;
        
        }

        public void up(int i)
        {
            if (i >= data.data.Count)
            {
                return;
            }
            ArrayList list = data.data;
            bundledata t = (bundledata)list[i];
            monster s = new monster(t);

            s.id = SByte.Parse(form.textBox72.Text);
            s.name = form.textBox71.Text;
            s.icon = SByte.Parse(form.textBox80.Text);
            s.job = (SByte)form.comboBox15.SelectedIndex;
            s._hp = short.Parse(form.textBox70.Text);
            s._mp = short.Parse(form.textBox69.Text);
            s._pact = short.Parse(form.textBox68.Text);
            s._mact = short.Parse(form.textBox67.Text);
            s._pdef = short.Parse(form.textBox66.Text);
            s._mdef = short.Parse(form.textBox65.Text);
            //s.effectDelayFrame;

            s._mov = short.Parse(form.textBox64.Text);
            s._exp = short.Parse(form.textBox73.Text);
            s.it1 = SByte.Parse(form.textBox74.Text);
            s.it2 = SByte.Parse(form.textBox75.Text);
            s.it3 = SByte.Parse(form.textBox76.Text);
            s.sk1 = SByte.Parse(form.textBox77.Text);
            s.sk2 = SByte.Parse(form.textBox78.Text);
            s.sk3 = SByte.Parse(form.textBox79.Text);

            t = s.tobundledata();
            list[i] = t;
        }

        public void add()
        {
            ArrayList list = data.data;

            monster s = new monster();

            s.id = SByte.Parse(form.textBox72.Text);
            s.name = form.textBox71.Text;
            s.icon = SByte.Parse(form.textBox80.Text);
            s.job = (SByte)form.comboBox15.SelectedIndex;
            s._hp = short.Parse(form.textBox70.Text);
            s._mp = short.Parse(form.textBox69.Text);
            s._pact = short.Parse(form.textBox68.Text);
            s._mact = short.Parse(form.textBox67.Text);
            s._pdef = short.Parse(form.textBox66.Text);
            s._mdef = short.Parse(form.textBox65.Text);
            //s.effectDelayFrame;

            s._mov = short.Parse(form.textBox64.Text);
            s._exp = short.Parse(form.textBox73.Text);
            s.it1 = SByte.Parse(form.textBox74.Text);
            s.it2 = SByte.Parse(form.textBox75.Text);
            s.it3 = SByte.Parse(form.textBox76.Text);
            s.sk1 = SByte.Parse(form.textBox77.Text);
            s.sk2 = SByte.Parse(form.textBox78.Text);
            s.sk3 = SByte.Parse(form.textBox79.Text);

            bundledata t = s.tobundledata();
            list.Add(t);
        }
    }

    public class skill
    {

        static SByte TYPE_ONE = 0;
        static SByte TYPE_ALL = 1;
        static SByte TYPE_ALL_SCATTERED = 2;

        static SByte GOAL_ENEMY = 0;
        static SByte GOAL_TEAM = 1;

        String name;

        String info;

        SByte id;

        SByte type;

        SByte goal;

        /** mp消耗 */
        short mp;

        /** 目标hp消耗 */
        short hp;

        /** 技能效果 */
        Sprite sprite;

        /** 是否有技能效果图（0没有，1有） */
        SByte skillAnimation;

        /** 受伤动作延迟 */
        SByte injuredDelay;

        /** 附加效果：（0没有，1屏幕震动，2屏幕闪烁，3屏幕变黑） */
        SByte effect;

        /** 特效延迟帧数 */
        SByte effectDelayFrame;

        SByte state;

        long stateLastTime;

        /** 成功率 */
        SByte pro;

        /** 攻击位置 */
        SByte attackPos;

        public bundle data = new bundle();
        mainForm form = null;
        private String fn;
        public skill(mainForm f)
        {
            form = f;
        }
        public skill()
        {
        }

        public skill(bundledata d)
        {
            this.id = sbyte.Parse(d.key);
            name = d.data[0];
            info = d.data[1];
            type = SByte.Parse(d.data[2]);
            goal = SByte.Parse(d.data[3]);
            pro = SByte.Parse(d.data[4]);
            mp = short.Parse(d.data[5]);
            skillAnimation = SByte.Parse(d.data[6]);
            injuredDelay = SByte.Parse(d.data[7]);
            effect = SByte.Parse(d.data[8]);
            effectDelayFrame = SByte.Parse(d.data[9]);

            hp = short.Parse(d.data[10]);
            state = SByte.Parse(d.data[11]);
            stateLastTime = SByte.Parse(d.data[12]);
            attackPos = SByte.Parse(d.data[13]);
        }

        public bundledata tobundledata()
        {
            bundledata d = new bundledata();
            d.data = new string[14];
            d.key = "" + id;

            d.data[0] = name;
            d.data[1] = info;
            d.data[2] = ""+type;
            d.data[3] = "" + goal;
            d.data[4] = "" + pro;
            d.data[5] = "" + mp;
            d.data[6] = "" + skillAnimation;
            d.data[7] = "" + injuredDelay;
            d.data[8] = "" + effect;
            d.data[9] = "" + effectDelayFrame;

            d.data[10] = "" + hp;
            d.data[11] = "" + state;
            d.data[12] = "" + stateLastTime;
            d.data[13] = "" + attackPos;
            return d;
        }

        public void setFn(String fn)
        {
            this.fn = fn;
            form.textBox54.Text = fn;
        }

        public String getFn()
        {
            return fn;
        }

        public void save()
        {
            data.save(fn);
        }

        public void getInfo(String fn)
        {

            data.load(fn);

        }
        public void init()
        {
            
        }
        public void display()
        {

            form.listBox12.Items.Clear();
            ArrayList list = data.data;
            for (int i = 0; i < list.Count; i++)
            {
                bundledata t = (bundledata)(list[i]);
                skill s = new skill(t);
                form.listBox12.Items.Add(s.id + "," + s.name);
            }
            if (list.Count > 0)
            {
                form.listBox12.SelectedIndex = 0;

                displayatt(0);
            }
        }

        public void displayatt(int i)
        {
            if (i >= data.data.Count)
            {
                return;
            }
            ArrayList list = data.data;
            bundledata t = (bundledata)list[i];
            skill s = new skill(t);
            form.textBox55.Text = "" + s.id;
            form.textBox56.Text = "" + s.name;
            form.textBox57.Text = "" + s.info;
            form.comboBox9.SelectedIndex = s.type;
            form.comboBox10.SelectedIndex = s.goal;
            form.textBox59.Text = "" + s.hp;
            form.textBox58.Text = "" + s.mp;
            form.comboBox11.SelectedIndex = s.skillAnimation;
            form.textBox60.Text = "" + s.injuredDelay;
            form.comboBox12.SelectedIndex = s.effect;
            //s.effectDelayFrame;

            form.textBox61.Text = "" + s.pro;
            form.comboBox13.SelectedIndex = s.state;
            form.textBox62.Text = "" + s.stateLastTime;
            form.comboBox14.SelectedIndex = s.attackPos;
        }

        public void up(int i)
        {
            if (i >= data.data.Count)
            {
                return;
            }
            ArrayList list = data.data;
            bundledata t = (bundledata)list[i];
            skill s = new skill(t);

            s.id = SByte.Parse(form.textBox55.Text);
            s.name = form.textBox56.Text;
            s.info = form.textBox57.Text;
            s.type = (SByte)form.comboBox9.SelectedIndex;
            s.goal = (SByte)(form.comboBox10.SelectedIndex);
            s.hp = SByte.Parse(form.textBox59.Text);
            s.mp = SByte.Parse(form.textBox58.Text);
            s.skillAnimation = (SByte)(form.comboBox11.SelectedIndex);
            s.injuredDelay = SByte.Parse(form.textBox60.Text);
            s.effect = (SByte)(form.comboBox12.SelectedIndex);
            //s.effectDelayFrame;

            s.pro = SByte.Parse(form.textBox61.Text);
            s.state = (SByte)(form.comboBox13.SelectedIndex);
            s.stateLastTime = SByte.Parse(form.textBox62.Text);
            s.attackPos = (SByte)(form.comboBox14.SelectedIndex);

            t = s.tobundledata();
            list[i] = t;
        }

        public void add()
        {
            ArrayList list = data.data;
           
            skill s = new skill();

            s.id = SByte.Parse(form.textBox55.Text);
            s.name = form.textBox56.Text;
            s.info = form.textBox57.Text;
            s.type = (SByte)form.comboBox9.SelectedIndex;
            s.goal = (SByte)(form.comboBox10.SelectedIndex);
            s.hp = SByte.Parse(form.textBox59.Text);
            s.mp = SByte.Parse(form.textBox58.Text);
            s.skillAnimation = (SByte)(form.comboBox11.SelectedIndex);
            s.injuredDelay = SByte.Parse(form.textBox60.Text);
            s.effect = (SByte)(form.comboBox12.SelectedIndex);
            //s.effectDelayFrame;

            s.pro = SByte.Parse(form.textBox61.Text);
            s.state = (SByte)(form.comboBox13.SelectedIndex);
            s.stateLastTime = SByte.Parse(form.textBox62.Text);
            s.attackPos = (SByte)(form.comboBox14.SelectedIndex);

            bundledata t = s.tobundledata();
            list.Add(t);
        }
    }
}
